package cn.org.spring.wechar.prize.service.impl;

import cn.org.spring.wechar.constant.PrizeConstant;
import cn.org.spring.wechar.prize.entity.RecTelPrize;
import cn.org.spring.wechar.prize.mapper.RecTelPrizeMapper;
import cn.org.spring.wechar.prize.service.RecTelPrizeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author： 谢辉
 * @date： 2021/5/20
 * @email: xieh_mail@163.com
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Slf4j
@Service
public class RecTelPrizeServiceImpl implements RecTelPrizeService {

    @Autowired
    RecTelPrizeMapper recTelPrizeMapper;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    @Override
    public List<RecTelPrize> selectAllByList() {
        Boolean telprize = redisTemplate.opsForHash().hasKey(PrizeConstant.PRIZE_HASHKEY, "telprizesdetail");
        if (telprize) {
            return (List<RecTelPrize>) redisTemplate.opsForHash().get(PrizeConstant.PRIZE_HASHKEY, "telprizesdetail");
        } else {
            List<RecTelPrize> recTelPrizes = recTelPrizeMapper.selectAllByList();
            redisTemplate.opsForHash().put(PrizeConstant.PRIZE_HASHKEY, "telprizesdetail", recTelPrizes);
            return recTelPrizes;
        }
    }

    @Override
    public RecTelPrize selectTelPrizeById(Long id) {
        if (id == null) {
            log.warn("根据id查询话费详细信息，id为空值，直接返回不处理。");
            return null;
        }

        Boolean hasKey = redisTemplate.opsForHash().hasKey(PrizeConstant.PRIZE_HASHKEY, "telprize:" + id);
        if (hasKey) {
            return (RecTelPrize) redisTemplate.opsForHash().get(PrizeConstant.PRIZE_HASHKEY, "telprize:" + id);
        } else {
            RecTelPrize recTelPrize = recTelPrizeMapper.selectTelPrizeById(id);
            redisTemplate.opsForHash().put(PrizeConstant.PRIZE_HASHKEY, "telprize:" + id, recTelPrize);
            return recTelPrize;
        }
    }

    @Override
    public int insert(RecTelPrize recTelPrize) {
        if (recTelPrize == null) {
            log.warn("插入话费奖品信息为空值，直接返回不处理。");
            return 0;
        }

        Long delete = redisTemplate.opsForHash().delete(PrizeConstant.PRIZE_HASHKEY, "telprizesdetail");
        log.info("新增话费奖品，内容是：【{}】，删除旧缓存。被删除的个数：【{}】", recTelPrize.toString(), delete);
        return recTelPrizeMapper.insert(recTelPrize);
    }

    @Override
    public int update(RecTelPrize recTelPrize) {
        if (recTelPrize == null || recTelPrize.getId() == null) {
            log.warn("更新话费奖品信息为空值，直接返回不处理。");
            return 0;
        }
        Long id = recTelPrize.getId();
        Long delete = redisTemplate.opsForHash().delete(PrizeConstant.PRIZE_HASHKEY, "telprize:" + id, "telprizesdetail");
        log.info("话费奖品被更新，内容是：【{}】，删除旧缓存。被删除的个数：【{}】", recTelPrize.toString(), delete);
        return recTelPrizeMapper.update(recTelPrize);
    }
}
